package com.jar.exercise.io.序列流;

import org.junit.Test;

import java.io.*;
import java.util.Enumeration;
import java.util.Vector;

/**
 * 序列流SequenceInputStream
 * 可以让多个流并成一个流
 */
public class SequenceDemo {
	@Test
	public void noBuffer() throws Exception {
		Vector<FileInputStream> list = new Vector<>();
		list.add(new FileInputStream("D:\\Study\\IDEA\\vue\\Demo\\src\\io\\序列流\\1.txt"));
		list.add(new FileInputStream("D:\\Study\\IDEA\\vue\\Demo\\src\\io\\序列流\\2.txt"));
		list.add(new FileInputStream("D:\\Study\\IDEA\\vue\\Demo\\src\\io\\序列流\\3.txt"));
		Enumeration<FileInputStream> elements = list.elements();
		SequenceInputStream sis = new SequenceInputStream(elements);
		OutputStream os = new FileOutputStream("D:\\Study\\IDEA\\vue\\Demo\\src\\io\\序列流\\4.txt");
		byte[] b = new byte[1024];
		int len;
		while ((len = sis.read(b)) != -1) {
			os.write(b, 0, len);
		}
		os.close();
		sis.close();
	}

	@Test
	public void buffer() throws Exception {
		Vector<FileInputStream> list = new Vector<>();
		list.add(new FileInputStream("D:\\Study\\IDEA\\vue\\Demo\\src\\io\\序列流\\1.txt"));
		list.add(new FileInputStream("D:\\Study\\IDEA\\vue\\Demo\\src\\io\\序列流\\2.txt"));
		list.add(new FileInputStream("D:\\Study\\IDEA\\vue\\Demo\\src\\io\\序列流\\3.txt"));
		Enumeration<FileInputStream> elements = list.elements();
		SequenceInputStream sis = new SequenceInputStream(elements);
		BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream("D:\\Study\\IDEA\\vue\\Demo\\src\\io\\序列流\\4.txt"));
		int len;
		while ((len = sis.read()) != -1) {
			os.write(len);
		}
		os.close();
		sis.close();
	}
}
